package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private HttpSession session;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        //1.获取登入人
        Long id = (Long) session.getAttribute("employee");

        //2.完善修改人,更新人
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);

        //3.调用业务层添加
        setmealService.save(setmealDto);

        //4.返回
        return R.success("添加套餐成功");
    }

    //套餐分页查询数据
    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        //1.调用业务层查询分页数据
        Page<SetmealDto> pageObj = setmealService.findByPage(page, pageSize, name);

        return R.success(pageObj);
    }

    //删除套餐
    @DeleteMapping
    //这个springmvc就将前端传递过来的数据封装成一个List集合,如果不加默认封装成数组
    public R<String> deleteByIds(@RequestParam List<Long> ids){
        //获取操作人
        Long id = (Long) session.getAttribute("employee");

        //调用业务层方法删除
        setmealService.deleteByIds(ids,id);

        //返回
        return R.success("删除套餐成功");
    }

    //根据套餐类别查询套餐
    @RequestMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        //调用业务层
        List<Setmeal> setmealList = setmealService.list(categoryId, status);

        //返回数据
        return R.success(setmealList);
    }
}
